#include chip.inc
#include vars.inc
	code
;-----------------------------------------------------------------;
;Begin Buffer_Init routine
;	Sets up the FIFO buffer
;	Prerequisites: 	None
;	Input:			None
;	Output:			None
;	Cycles:			329 (with 80 long buffer)
;	Bank change:	No
Buffer_Init:
	GLOBAL 	Buffer_Init
	;SETUP INDIRECT FILE REGISTERS
	LFSR	ACTION_BUFFER_FSR,ACTION_BUFFER_LOCATION
	LFSR	DATA_BUFFER_FSR,DATA_BUFFER_LOCATION
	
	;CLEAR BUFFER ACTION MEMORY
	MOVLW	BUFFER_LENGTH;3
CLEARING_ACTIONS:
	CLRF	ACTION_BUFFER_W,A
	DECFSZ	WREG,W,A
	BRA		CLEARING_ACTIONS
	CLRF	ACTION_BUFFER_W,A
	
	;AND SET POINTERS TO FIRST SLOT
	MOVLW	BUFFER_LENGTH
	MOVWF	BUFFER_LAST,A
	MOVWF	BUFFER_FIRST,A
	
	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_Pop routine
;	Gets and removes the next element out of the FIFO buffer
;	Prerequisites: 	None
;	Input:			None
;	Output:			Action 	-> BUFFER_POP_ACTION
;					Data	-> buffer_pop_data
;	Cycles:			7 - 10(RARE)
;	Bank change:	No
Buffer_Pop
	global Buffer_Pop
	
	MOVF	BUFFER_FIRST,W,A
	MOVFF	DATA_BUFFER_W,BUFFER_POP_DATA
	;NOP
	MOVFF	ACTION_BUFFER_W,BUFFER_POP_ACTION
	;NOP
	
	;INCREMENT THE COUNTER
	DECFSZ	BUFFER_FIRST,F,A
	RETURN

	MOVLW	BUFFER_LENGTH
	MOVWF	BUFFER_FIRST,A
	RETURN
;	
;	;IF WE HAVE REACHED THE END, WRAP AROUND
;	MOVF	BUFFER_FIRST,W,A
;	SUBLW	BUFFER_LENGTH
;	BTFSC	STATUS,Z,A		
;	CLRF	BUFFER_FIRST,A
;	
;	;CHECK FOR BUFFER UNDERFLOW AND SIGNAL IT WITH LEDS
;	MOVF	BUFFER_LAST,W,A
;	SUBWF	BUFFER_FIRST,W,A
;	BTFSC	STATUS,Z,A
;	BSF		LATC,YELLOW,A
;	
	RETURN
	
	
;-----------------------------------------------------------------;
;Begin Buffer_Peek_Action routine
;	Gets the next element in the FIFO buffer
;	Prerequisites: 	None
;	Input:			None
;	Output:			Action 	-> W
;					0x00 	-> W If no data
;	Cycles:			7
;	Bank change:	No
Buffer_Peek_Action
	global Buffer_Peek_Action
	MOVF	BUFFER_FIRST,W,A
	SUBWF	BUFFER_LAST,W,A
	BZ		BUFFER_PEEK_ACTION_NONE
	
	MOVF	BUFFER_FIRST,W,A	
	MOVF	ACTION_BUFFER_W,W,A
	RETURN
BUFFER_PEEK_ACTION_NONE
	CLRF	WREG
	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_Drop routine
;	Removes the next element from the FIFO buffer
;	Prerequisites: 	None
;	Input:			None
;	Output:			None
;	Cycles:			3-6(RARE)
;	Bank change:	No
Buffer_Drop
	GLOBAL Buffer_Drop
	;INCREMENT THE COUNTER
;	INCF	BUFFER_FIRST,F,A
	DECFSZ	BUFFER_FIRST,F,A
	RETURN
	
	MOVLW	BUFFER_LENGTH
	MOVWF	BUFFER_FIRST,A
	RETURN
;	;IF WE HAVE REACHED THE END, WRAP AROUND
;	MOVF	BUFFER_FIRST,W,A
;	SUBLW	BUFFER_LENGTH
;	BTFSC	STATUS,Z,A		
;	CLRF	BUFFER_FIRST,A
;	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_Peek_Data routine
;	Gets the next element's Data in the FIFO buffer
;	Prerequisites: 	None
;	Input:			None
;	Output:			Data 	-> W
;	Cycles:			4
;	Bank change:	No
Buffer_Peek_Data
	GLOBAL Buffer_Peek_Data
	MOVF	BUFFER_FIRST,W,A	
	MOVF	DATA_BUFFER_W,W,A
	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_Push_Action routine
;	Adds an element with no data to the FIFO buffer
;	Prerequisites: 	None
;	Input:			Action 	<- BUFFER_PUSH_ACTION
;	Output:			None
;	Cycles:			6-9(RARE)
;	Bank change:	No
Buffer_Push_Action
	global Buffer_Push_Action
	MOVF	BUFFER_LAST,W,A
	MOVFF	BUFFER_PUSH_ACTION,ACTION_BUFFER_W
	;NOP
	
	;DECREMENTS THE COUNTER
	DECFSZ	BUFFER_LAST,F,A
	RETURN
	
	MOVLW 	BUFFER_LENGTH
	MOVWF	BUFFER_LAST
	RETURN
;	;IF WE HAVE REACHED THE END, WRAP AROUND
;	MOVF	BUFFER_LAST,W,A
;	SUBLW	BUFFER_LENGTH
;	BTFSC	STATUS,Z,A		
;	CLRF	BUFFER_LAST,A
;	
;	;CHECK FOR BUFFER OVERFLOW AND SIGNAL IT WITH LEDS
;	MOVF	BUFFER_LAST,W,A
;	SUBWF	BUFFER_FIRST,W,A
;	BTFSC	STATUS,Z,A
;	BSF		LATC,RED,A
;	
;	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_Push routine
;	Adds an element to the FIFO buffer
;	Prerequisites: 	None
;	Input:			Action 	<- BUFFER_PUSH_ACTION
;					Data	<- buffer_push_data
;	Output:			None
;	Cycles:			8-11(RARE)
;	Bank change:	No
Buffer_Push
	global Buffer_Push
	
	MOVF	BUFFER_LAST,W,A
	MOVFF	BUFFER_PUSH_ACTION,ACTION_BUFFER_W
	;NOP
	MOVFF	BUFFER_PUSH_DATA,DATA_BUFFER_W
	;NOP
	
	;DECREMENT THE COUNTER
	DECFSZ	BUFFER_LAST,F,A
	RETURN

	MOVLW	BUFFER_LENGTH
	MOVWF	BUFFER_LAST,A
	RETURN
	
;	;IF WE HAVE REACHED THE END, WRAP AROUND
;	MOVF	BUFFER_LAST,W,A
;	SUBLW	BUFFER_LENGTH
;	BTFSC	STATUS,Z,A		
;	CLRF	BUFFER_LAST,A
;	
;	;CHECK FOR BUFFER OVERFLOW AND SIGNAL IT WITH LEDS
;	MOVF	BUFFER_LAST,W,A
;	SUBWF	BUFFER_FIRST,W,A
;	BTFSC	STATUS,Z,A
;	BSF		LATC,RED,A
;	
;	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_Has_Data routine
;	Check the status of the FIFO buffer
;	Prerequisites: 	None
;	Input:			None
;	Output:			W		(0x00 if no data)
;					Status,Z(Z==1 if no data)
;	Cycles:			4
;	Bank change:	No
Buffer_Has_Data
	global Buffer_Has_Data
	
	;IF THERE IS DATA, POINTER WILL NOT MATCH
	MOVF	BUFFER_LAST,W,A
	SUBWF	BUFFER_FIRST,W,A
	RETURN
	
;-----------------------------------------------------------------;
;Begin Buffer_ReAssign_Next routine
;	Change the action of the next element in the FIFO buffer
;	Prerequisites: 	None
;	Input:			Action 	<- BUFFER_PUSH_ACTION
;	Output:			Non
;	Cycles:			4
;	Bank change:	No
Buffer_ReAssign_Next
	global Buffer_ReAssign_Next
	
	MOVF	BUFFER_FIRST,W,A
	MOVFF	BUFFER_PUSH_ACTION,ACTION_BUFFER_W	
	RETURN
	end